package com.wnxy.controller;

import com.wnxy.pojo.User;
import com.wnxy.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@Api(tags = "用户管理")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping
    @ApiOperation("查询全部")
    public List<User> findAll(){
        return userService.findAll();
    }


    @PostMapping
    @ApiOperation("添加或修改")
    public Boolean saveOrUpdate(@RequestBody User User) {
        if (StringUtils.isEmpty(User.getId())) {
            // id 为空，执行添加
            userService.add(User);
        } else {
            // id 不为空，说明是修改
            userService.update(User);
        }
        return true;
    }

    @DeleteMapping("/{id}")
    @ApiOperation("查询全部")
    public Boolean delete(@PathVariable("id") Integer id){
        userService.delete(id);
        return true;
    }


    @PutMapping("/del/batch")
    @ApiOperation("查询全部")
    public Boolean delBatch(@RequestBody List<Integer> ids){
        userService.delBatch(ids);
        return true;
    }


}















